//
//  DLRecordListCell.m
//  DontRem
//
//  Created by apple on 2021/1/22.
//

#import "DLRecordListCell.h"
#import "ImageFactory.h"
#define pageFontColor TextColor
@interface DLRecordListCell()
@property(nonatomic,retain)UILabel * titleLab ,* accountLab, * pswLab, * urlLab , * remarkLab, * expirateLab;
@property(nonatomic,retain)UIView * line, * urlView, * remarkView;
@property(nonatomic,retain)UIImageView * iconView;
@end
@implementation DLRecordListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = UIColorClear;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createCusUI];
    }
    return self;
}
-(void)eyeBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.isSelected) {
        self.pswLab.text = self.model.password;
    }else{
        NSString * str = @"";
        NSInteger count = self.model.password.length <= 8 ?self.model.password.length :8;
        for (NSInteger i =0; i<count; i++) {
            str = [str stringByAppendingString:@"*"];
        }
        self.pswLab.text = str;
    }
}
- (void)setModel:(KdbEntry *)model{
    _model = model;
    self.titleLab.text = model.title;
    self.accountLab.text = model.username;
    NSString * str = @"";
    NSInteger count = self.model.password.length <= 8 ?self.model.password.length :8;
    for (int i =0; i<count; i++) {
        str = [str stringByAppendingString:@"*"];
    }
    self.pswLab.text = str;
    NSString * datestr = [Mytools dateToDateStr:model.expiryTime];
    if (datestr.length > 10) {
        datestr = [datestr substringToIndex:10];
        self.expirateLab.text = datestr;
    }
    UIImage *image = [[ImageFactory sharedInstance] imageForIndex:model.image];
    self.iconView.image = image;
    if (model.url.length > 0) {
        self.urlLab.text = model.url;
        [self.urlView setupAutoHeightWithBottomView:self.urlLab bottomMargin:0];
        self.urlView.hidden = NO;
    }else{
        self.urlView.mm_height(0.01);
        self.urlView.hidden = YES;
    }
    
    if (model.notes.length > 0) {
        self.remarkLab.text = model.notes;
        [self.remarkView setupAutoHeightWithBottomView:self.remarkLab bottomMargin:0];
        self.remarkView.hidden = NO;
        if (self.urlView.isHidden) {
            self.remarkView.sd_layout.topSpaceToView(self.pswLab, 10);
        }else{
            self.remarkView.sd_layout.topSpaceToView(self.urlView, 10);
        }
    }else{
        self.remarkView.mm_height(0.01);
        self.remarkView.hidden = YES;
    }
    
    if (self.remarkView.isHidden) {
        self.line.sd_layout.topSpaceToView(self.urlView.isHidden?self.pswLab:self.urlView, 10);
    }else{
        self.line.sd_layout.topSpaceToView(self.remarkView, 10);
    }
}
-(void)createCusUI{
    UIView * bgView = [UIView new];
    bgView.backgroundColor = UIColorWhite;
    bgView.cornerRadius = 5;
    [self.contentView addSubview:bgView];
    bgView.sd_layout.leftSpaceToView(self.contentView, 10).rightSpaceToView(self.contentView, 10).topSpaceToView(self.contentView, 10);
    
    CGFloat titleHeight = floor(AUTO(18));
    
    self.expirateLab = [UILabel labelWithTitle:@"" color:pageFontColor fontSize:titleHeight];
    [bgView addSubview:self.expirateLab];
    self.expirateLab.sd_layout.rightSpaceToView(bgView, 10).topSpaceToView(bgView, 15).heightIs(titleHeight + 2);
    [self.expirateLab setSingleLineAutoResizeWithMaxWidth:300];
    
    self.iconView = [UIImageView new];
    [bgView addSubview:self.iconView];
    self.iconView.sd_layout.leftSpaceToView(bgView, 10).topSpaceToView(bgView, 15).widthIs(20).heightEqualToWidth();
    
    self.titleLab = [UILabel labelWithTitle:@"" color:pageFontColor fontSize:titleHeight alignment:NSTextAlignmentLeft];
    [bgView addSubview:self.titleLab];
    self.titleLab.sd_layout.leftSpaceToView(self.iconView, 5).topSpaceToView(bgView, 15).rightSpaceToView(self.expirateLab, 10).autoHeightRatio(0);
    
    UILabel * accountTmp = [UILabel labelWithTitle:[NSString stringWithFormat:@"%@：",NSLocalizedString(@"User Name", nil)] color:pageFontColor fontSize:titleHeight-2];
    [bgView addSubview:accountTmp];
    accountTmp.sd_layout.leftEqualToView(self.iconView).topSpaceToView(self.titleLab, 10).heightIs(titleHeight);
    [accountTmp setSingleLineAutoResizeWithMaxWidth:300];
    
    self.accountLab = [UILabel labelWithTitle:@"" color:pageFontColor fontSize:titleHeight-2];
    [bgView addSubview:self.accountLab];
    self.accountLab.sd_layout.leftSpaceToView(accountTmp, 0).centerYEqualToView(accountTmp).heightIs(titleHeight);
    [self.accountLab setSingleLineAutoResizeWithMaxWidth:300];
    
    UIButton * eyeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [eyeBtn setImage:Image(@"eye_close") forState:UIControlStateNormal];
    [eyeBtn setImage:Image(@"eye_open") forState:UIControlStateSelected];
    [eyeBtn addTarget:self action:@selector(eyeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:eyeBtn];
    eyeBtn.sd_layout.rightSpaceToView(bgView, 5).topSpaceToView(self.accountLab, 10).widthIs(30).heightEqualToWidth();
    
    UILabel * pswTmp = [UILabel labelWithTitle:[NSString stringWithFormat:@"%@：",NSLocalizedString(@"Password", nil)] color:pageFontColor fontSize:titleHeight-2];
    [bgView addSubview:pswTmp];
    pswTmp.sd_layout.leftEqualToView(self.iconView).centerYEqualToView(eyeBtn).heightIs(titleHeight);
    [pswTmp setSingleLineAutoResizeWithMaxWidth:300];
    
    self.pswLab = [UILabel labelWithTitle:@"" color:pageFontColor fontSize:titleHeight-2 alignment:NSTextAlignmentLeft];
    [bgView addSubview:self.pswLab];
    self.pswLab.sd_layout.leftSpaceToView(pswTmp, 0).centerYEqualToView(eyeBtn).rightSpaceToView(eyeBtn, 5).autoHeightRatio(0);
    
    self.urlView = [UIView new];
    [bgView addSubview:self.urlView];
    self.urlView.sd_layout.leftEqualToView(bgView).rightEqualToView(bgView).topSpaceToView(pswTmp, 10);
        
    UILabel * urlTmp = [UILabel labelWithTitle:[NSString stringWithFormat:@"%@：",NSLocalizedString(@"Web Site", nil)] color:pageFontColor fontSize:titleHeight-2];
    [self.urlView addSubview:urlTmp];
    urlTmp.sd_layout.leftSpaceToView(self.urlView,10).topSpaceToView(self.urlView, 0).heightIs(titleHeight);
    [urlTmp setSingleLineAutoResizeWithMaxWidth:300];
    
    self.urlLab = [UILabel labelWithTitle:@"" color:pageFontColor fontSize:titleHeight-2 alignment:NSTextAlignmentLeft];
    [self.urlView addSubview:self.urlLab];
    self.urlLab.sd_layout.leftSpaceToView(urlTmp, 0).topEqualToView(urlTmp).rightSpaceToView(self.urlView, 10).autoHeightRatio(0);
    
    
    self.remarkView = [UIView new];
    [bgView addSubview:self.remarkView];
    self.remarkView.sd_layout.leftEqualToView(bgView).rightEqualToView(bgView).topSpaceToView(self.urlView, 10);
    
    UILabel * remarkTmp = [UILabel labelWithTitle:[NSString stringWithFormat:@"%@：",NSLocalizedString(@"Note", nil)] color:pageFontColor fontSize:titleHeight-2];
    [self.remarkView addSubview:remarkTmp];
    remarkTmp.sd_layout.leftSpaceToView(self.remarkView,10).topSpaceToView(self.remarkView, 0).heightIs(titleHeight);
    [remarkTmp setSingleLineAutoResizeWithMaxWidth:300];
    self.remarkLab = [UILabel labelWithTitle:@"" color:pageFontColor fontSize:titleHeight-2 alignment:NSTextAlignmentLeft];
    [self.remarkView addSubview:self.remarkLab];
    self.remarkLab.sd_layout.leftSpaceToView(remarkTmp, 0).topEqualToView(remarkTmp).rightSpaceToView(self.remarkView, 10).autoHeightRatio(0);
    
    self.line = [UIView new];
    self.line.backgroundColor = UnderLineColor;
    [bgView addSubview:self.line];
    self.line.sd_layout.leftEqualToView(bgView).rightEqualToView(bgView).topSpaceToView(self.remarkView, 10).heightIs(0.7);
    
    NSArray * arr = @[NSLocalizedString(@"Copy url", nil),NSLocalizedString(@"Copy account", nil),NSLocalizedString(@"Copy password", nil)];
    NSArray * cArr = @[AssistColor,AssistBlueColor,CertainBtnColor];
    CGFloat wid = (SCREEN_WIDTH -20- 40)/3.0;
    UIButton * lastBtn = nil;
    for (int i =0; i<arr.count ; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorWhite forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor normalColor:cArr[i] darkColor:UIColorGrayDarken];
        btn.titleLabel.font = UIFontMake(titleHeight -2);
        btn.cornerRadius = 5;
        btn.tag = 10 + i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:btn];
        btn.sd_layout.leftSpaceToView(lastBtn?:bgView, 10).topSpaceToView(self.line, 15).widthIs(wid).heightIs(35);
        lastBtn = btn;
    }
    
    [bgView setupAutoHeightWithBottomView:lastBtn bottomMargin:15];
    [self setupAutoHeightWithBottomView:bgView bottomMargin:0];
}
-(void)btnClick:(UIButton *)sender{
    UIPasteboard * board = [UIPasteboard generalPasteboard];
    if (sender.tag == 10) {
        if (self.model.url.length == 0) {
            XHQHUDText(NSLocalizedString(@"Not url's record", nil));
        }else{
            board.string = self.model.url;
            XHQHUDSuccessText(NSLocalizedString(@"Copied", nil));
        }
    }else if(sender.tag == 11){
        board.string = self.model.username;
        XHQHUDSuccessText(NSLocalizedString(@"Copied", nil));
    }else{
        board.string = self.model.password;
        XHQHUDSuccessText(NSLocalizedString(@"Copied", nil));
    }
}

@end
